﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AutoEditUI.UserControls
{
    /// <summary>
    /// QRProp.xaml 的交互逻辑
    /// </summary>
    public partial class QRProp : UserControl
    {
        public QRProp()
        {
            InitializeComponent();
        }
        public MainWindow gmainWindow { get; private set; }
        public Image gx { get; private set; }
        public UIItem gUIItem { get; private set; }
        internal void Bind(MainWindow mainWindow, Image x)
        {
            gUIItem = (UIItem)mainWindow.GeItemByTag((string)x.Tag);
            localrelate.SetItm(mainWindow, gUIItem, x);
            TextBlockTag.Text = x.Tag.ToString();
            TextBoxW.Text = x.Width.ToString();
            TextBoxH.Text = x.Height.ToString();
            ItemQR mItemQR = (ItemQR)mainWindow.GeItemByTag((string)x.Tag);
            gx = x;
            gmainWindow = mainWindow;
            TextBlockTag.Text = x.Tag.ToString();
            TextBoxW.Text = x.Width.ToString();
            TextBoxH.Text = x.Height.ToString();
            TextBoxValue.Text = mItemQR.ItemValue;
            x.SetBinding(Image.WidthProperty, new Binding("Text") { Source = TextBoxW });
            x.SetBinding(Image.HeightProperty, new Binding("Text") { Source = TextBoxH });
            X.Text = x.GetValue(Canvas.LeftProperty).ToString();
            Y.Text = x.GetValue(Canvas.TopProperty).ToString();


            Binding xxx = new Binding("Text") { Source = X };
            xxx.Mode = BindingMode.TwoWay;
            x.SetBinding(Canvas.LeftProperty, xxx);

            Binding yyy = new Binding("Text") { Source = Y };
            yyy.Mode = BindingMode.TwoWay;
            x.SetBinding(Canvas.TopProperty, yyy);

            TextBoxName.Text = x.Name.ToString();
            x.SetBinding(Image.NameProperty, new Binding("Text") { Source = TextBoxName });


        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            foreach (ItemQR item in gmainWindow.gUIModel.ItemQRs)
            {
                if (item.Tag == (string)gx.Tag)
                {
                    item.ItemValue = TextBoxValue.Text;
                    UpdateQR(TextBoxValue.Text);
                    return;
                }
            }
            MessageBox.Show("No " + (string)gx.Tag);
        }
        internal void UpdateQR(string ItemValue)
        {

            gx.Source = Utils.WorkForSVG(ItemValue, 50);

        }
    }

    
}
